home *** CD-ROM | disk | FTP | other *** search
- /*
- * (c) Copyright 1992 by Panagiotis Tsirigotis
- * All rights reserved. The file named COPYRIGHT specifies the terms
- * and conditions for redistribution.
- */
-
- static char RCSid[] = "$Id: msg.c,v 5.1 1992/11/01 00:01:21 panos Exp $" ;
-
- #include <syslog.h>
- #include <fcntl.h>
-
- #include "sio.h"
- #include "str.h"
- #include "xlog.h"
-
- #include "options.h"
-
- #include "defs.h"
- #include "state.h"
- #include "config.h"
-
-
- static struct name_value priorities[] =
- {
- { "WARNING", LOG_WARNING },
- { "ERROR", LOG_ERR },
- { "CRITICAL", LOG_CRIT },
- { "NOTICE", LOG_NOTICE },
- { "DEBUG", LOG_DEBUG },
- { "INFO", LOG_INFO },
- { NULL, 0 }
- } ;
-
-
- extern int line_count ;
- extern struct name_value syslog_facilities[] ;
-
- void msg() ;
-
- #define BUFSIZE 2048
-
- #define DEFAULT_SYSLOG_LEVEL LOG_INFO
-
-
- status_e msg_init()
- {
- xlog_h xh ;
- bool_int facility_error = FALSE ;
- int fd = -1 ;
- char *func = "msg_init" ;
-
- if ( debug.on )
- xh = xlog_create( XLOG_FILELOG, program_name, XLOG_NOFLAGS,
- "/dev/tty", O_APPEND + O_WRONLY, 0 ) ;
- else
- {
- if ( filelog_option )
- xh = xlog_create( XLOG_FILELOG, program_name,
- XLOG_PRINT_TIMESTAMP + XLOG_PRINT_ID + XLOG_PRINT_PID,
- filelog_option_arg, LOG_OPEN_FLAGS, LOG_FILE_MODE ) ;
- else
- {
- int facility = DEFAULT_SYSLOG_FACILITY ;
-
- if ( syslog_option )
- {
- struct name_value *nvp ;
-
- nvp = nv_find_value( syslog_facilities, syslog_option_arg ) ;
- if ( nvp != NULL )
- facility = nvp->value ;
- else
- facility_error = TRUE ;
- }
-
- xh = xlog_create( XLOG_SYSLOG, program_name, XLOG_NOFLAGS,
- facility, DEFAULT_SYSLOG_LEVEL ) ;
- }
- }
-
- if ( xh == NULL )
- return( FAILED ) ;
-
- xlog_control( xh, XLOG_GETFD, &fd ) ;
- if ( fd != -1 )
- {
- if ( fcntl( fd, F_SETFD, 1 ) == -1 )
- {
- xlog_destroy( xh ) ;
- return( FAILED ) ;
- }
- }
-
- ps.rws.program_log = xh ;
-
- if ( facility_error )
- msg( LOG_ERR, func, "Bad syslog facility: %s", syslog_option_arg ) ;
- return( OK ) ;
- }
-
-
- void msg_suspend()
- {
- xlog_control( ps.rws.program_log, XLOG_PREEXEC ) ;
- }
-
-
- void msg_resume()
- {
- xlog_control( ps.rws.program_log, XLOG_POSTEXEC ) ;
- }
-
-
- /*
- * The size argument is a value-result argument
- */
- PRIVATE int prepare_buffer( level, func, buf, size, fmt, ap )
- int level ;
- char *func ;
- char *buf ;
- unsigned size ;
- char *fmt ;
- va_list ap ;
- {
- char *bufstart = buf ;
- unsigned bytes_left = size ;
- int cc ;
-
- /*
- * Check if we need to print the level name
- */
- if ( debug.on || filelog_option )
- {
- struct name_value *nvp = nv_find_name( priorities, level ) ;
- char *pri = nvp ? nvp->name : "UNKNOWN" ;
-
- cc = strx_nprint( bufstart, bytes_left, "%s: ", pri ) ;
- bufstart += cc ;
- bytes_left -= cc ;
- }
-
- /*
- * Check if we need to print the function name
- */
- if ( debug.on || level == LOG_CRIT )
- {
- cc = strx_nprint( bufstart, bytes_left, "{%s} ", func ) ;
- bufstart += cc ;
- bytes_left -= cc ;
- }
-
- cc = strx_nprintv( bufstart, bytes_left, fmt, ap ) ;
-
- bytes_left -= cc ;
-
- return( size - bytes_left ) ;
- }
-
-
- /* VARARGS3 */
- void msg( level, func, fmt, va_alist )
- int level ;
- char *func ;
- char *fmt ;
- va_dcl
- {
- va_list ap ;
- char buf[ BUFSIZE ] ;
- int len ;
-
- va_start( ap ) ;
- len = prepare_buffer( level, func, buf, sizeof( buf ), fmt, ap ) ;
- va_end( ap ) ;
-
- xlog_write( ps.rws.program_log, buf, len, XLOG_SET_LEVEL, level ) ;
- }
-
-
- /*
- * Parser message.
- * There are 2 differences from msg():
- * 1) parsemsg() prints the line #
- * 2) parsemsg() does not interpret %m
- */
- /* VARARGS3 */
- void parsemsg( msg_level, func, fmt, va_alist )
- int msg_level ;
- char *func ;
- char *fmt ;
- va_dcl
- {
- va_list ap ;
- char buf[ BUFSIZE ] ;
- int cc ;
- int len ;
-
- va_start( ap ) ;
- len = prepare_buffer( msg_level, func, buf, sizeof( buf ), fmt, ap ) ;
- va_end( ap ) ;
-
- cc = strx_nprint( &buf[ len ], sizeof(buf)-len, " [line=%d]", line_count ) ;
- len += cc ;
-
- xlog_write( ps.rws.program_log, buf, len,
- XLOG_NO_ERRNO + XLOG_SET_LEVEL, msg_level ) ;
- }
-
-